package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.contans.user.UserContans;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.feign.ApAuthorFeign;
import com.heima.user.feign.WmUserFeign;
import com.heima.user.mapper.AdUserRealNameMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.AdUserRealNameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Transactional
@Service
public class AdUserRealNameServiceImpl extends ServiceImpl<AdUserRealNameMapper , ApUserRealname> implements AdUserRealNameService {


    @Override
    public ResponseResult loadListByStatus(AuthDto authDto) {
        if(authDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        authDto.checkParam();
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        if(authDto.getStatus() != null){
            wrapper.eq(ApUserRealname::getStatus, authDto.getStatus());
        }

        IPage page = new Page(authDto.getPage(), authDto.getSize());
        IPage page1 = page(page, wrapper);
        ResponseResult result = new PageResponseResult(authDto.getPage() , authDto.getSize() , (int)page1.getTotal());
        result.setData(page1.getRecords());
        return result;

    }

    @GlobalTransactional
    @Override
    public ResponseResult updateStatusById(AuthDto authDto, Short status) {
        //判断参数
        if(authDto == null || authDto.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查状态
        if (checkStatus(status)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //修改状态
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setStatus(status);
        apUserRealname.setId(authDto.getId());
        if(authDto.getMsg() != null || status.equals(UserContans.FAIL_AUTH)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        updateById(apUserRealname);
        //如果状态是通过将用户表数据存入到自媒体和作者表中
        if(status.equals(UserContans.PASS_AUTH)){
            ResponseResult result = createWmUserAndAuthor(authDto);
            if(result != null){
                return result;
            }
        }
        //int i = 1/0;
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private WmUserFeign wmUserFeign;

    private ResponseResult createWmUserAndAuthor(AuthDto authDto) {
        Integer id = authDto.getId();
        ApUserRealname apUserRealname = getById(id);
        Integer userId = apUserRealname.getUserId();
        ApUser apUser = apUserMapper.selectById(userId);
        if(apUser == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmUser wmUser = wmUserFeign.findByName(apUser.getName());
        if(wmUser == null){
            wmUser = new WmUser();
            wmUser.setName(apUser.getName());
            wmUser.setCreatedTime(new Date());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setPhone(apUser.getPhone());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setStatus(9);
            wmUser.setApUserId(apUser.getId());
            wmUserFeign.save(wmUser);
        }
        createAuthor(wmUser);

        apUser.setFlag((short)1);
        apUserMapper.updateById(apUser);
        return null;

    }

    @Autowired
    private ApAuthorFeign apAuthorFeign;

    private void createAuthor(WmUser wmUser) {
        Integer id = wmUser.getApUserId();
        ApAuthor apAuthor = apAuthorFeign.findById(id);
        if(apAuthor == null){
            apAuthor = new ApAuthor();
            apAuthor.setCreatedTime(new Date());
            apAuthor.setName(wmUser.getName());
            apAuthor.setType(UserContans.AUTH_TYPE);
            apAuthor.setUserId(wmUser.getApUserId());
            apAuthor.setWmUserId(wmUser.getId());
            apAuthorFeign.save(apAuthor);
        }
    }


    private boolean checkStatus(Short status) {
        if(status == null || (!status.equals(UserContans.PASS_AUTH)&&!status.equals(UserContans.FAIL_AUTH))){
            return true;
        }
        return false;
    }
}
